Source Code Viewer - Example 4 - Model

package name.matthewgreet.example4.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;

import name.matthewgreet.example4.exception.ConfigurationException;

public class Model {
	public class CheckTableWork implements Work {

		@Override
		public void execute(Connection connection) throws SQLException {
			PreparedStatement statement;
			ResultSet resultSet;
			
			statement = connection.prepareStatement("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_name = 'GAME'");
			resultSet = statement.executeQuery();
			resultSet.next();
			Model.this.databaseExists = resultSet.getLong(1) > 0 ;
		}
		
	}
	
	private static Model instance;
	
	public static synchronized Model getInstance() throws ConfigurationException {
		if (instance == null) {
			instance = new Model(BootstrapConfiguration.getInstance());
		}
		return instance;
	}
	
	private boolean databaseExists;
	private String databaseDirectory;
	private SessionFactory sessionFactory;
	
	private DesignerGameTable designerGameTable;
	private DesignerTable designerTable;
	private GameTable gameTable;
	private GameRatingTable gameRatingTable;
	private UserRatingTable userRatingTable;
	private UserTable userTable;
	
	private Model(BootstrapConfiguration configuration) {
		Metadata metadata;
		MetadataSources metadataSources;
		ServiceRegistry serviceRegistry;
		Map<String, String> settings;
		
		settings = makeDatabaseSettings(configuration);
		databaseDirectory = configuration.getDatabaseDirectory();
		serviceRegistry = new StandardServiceRegistryBuilder().applySettings(settings).build();
		
		metadataSources = new MetadataSources(serviceRegistry);
		addAnnotatedClasses(metadataSources);
		metadata = metadataSources.buildMetadata();
		sessionFactory = metadata.getSessionFactoryBuilder().build();
        init();
	}
	
	
	private void addAnnotatedClasses(MetadataSources metadataSources) {
		metadataSources.addAnnotatedClass(Artist.class);
		metadataSources.addAnnotatedClass(ArtistGame.class);
		metadataSources.addAnnotatedClass(Designer.class);
		metadataSources.addAnnotatedClass(DesignerGame.class);
		metadataSources.addAnnotatedClass(Game.class);
		metadataSources.addAnnotatedClass(Mechanic.class);
		metadataSources.addAnnotatedClass(GameRating.class);
		metadataSources.addAnnotatedClass(Publisher.class);
		metadataSources.addAnnotatedClass(PublisherGame.class);
		metadataSources.addAnnotatedClass(User.class);
		metadataSources.addAnnotatedClass(UserRating.class);
	}
	
	private boolean checkExistingTables() {
		Session session;
		Transaction transaction;
		
		session = sessionFactory.getCurrentSession(); 
		transaction = session.beginTransaction();
		session.doWork(new CheckTableWork());
		transaction.commit();
		return databaseExists;
	}
	
	private void init() {
		checkExistingTables();
	}
	


	private Map<String, String> makeDatabaseSettings(BootstrapConfiguration configuration) {
		  Map<String, String> result;
		  
		  result = new HashMap<>();
		  result.put("hibernate.connection.driver_class", "org.h2.Driver");
		  result.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
		  result.put("hibernate.connection.url", "jdbc:h2:file:" + System.getProperty("catalina.base") + "/webapps/example4/WEB-INF/classes/database;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO");
		  result.put("hibernate.connection.username", "sa");
		  result.put("hibernate.connection.password", "");
		  result.put("hibernate.current_session_context_class", "thread");
		  result.put("hibernate.show_sql", "false");
		  result.put("hibernate.format_sql", "false");
		  return result;
    }
	
	Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public boolean getDatabaseExists() {
		return databaseExists;
	}

	public String getDatabaseDirectory() {
		return databaseDirectory;
	}


	public synchronized DesignerGameTable getDesignerGameTable() {
		if (designerGameTable == null) {
			designerGameTable = new DesignerGameTable(sessionFactory);
		}
		return designerGameTable;
	}

	public synchronized DesignerTable getDesignerTable() {
		if (designerTable == null) {
			designerTable = new DesignerTable(sessionFactory);
		}
		return designerTable;
	}

	public synchronized GameTable getGameTable() {
		if (gameTable == null) {
			gameTable = new GameTable(sessionFactory);
		}
		return gameTable;
	}

	public synchronized GameRatingTable getGameRatingTable() {
		if (gameRatingTable == null) {
			gameRatingTable = new GameRatingTable(sessionFactory);
		}
		return gameRatingTable;
	}

	public synchronized UserRatingTable getUserRatingTable() {
		if (userRatingTable == null) {
			userRatingTable = new UserRatingTable(sessionFactory);
		}
		return userRatingTable;
	}

	public synchronized UserTable getUserTable() {
		if (userTable == null) {
			userTable = new UserTable(sessionFactory);
		}
		return userTable;
	}

}